<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单据编号" prop="vBillNo">
        <el-input
          v-model="queryParams.vBillNo"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮递编号" prop="vPostNo">
        <el-input
          v-model="queryParams.vPostNo"
          placeholder="请输入邮递编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮递方式" prop="vPostType">
        <el-select
          v-model="queryParams.vPostType"
          placeholder="请选择邮递方式"
          clearable
        >
          <el-option
            v-for="dict in dict.type.buy_post_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.inv_arrival_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据编号" align="center" prop="vBillNo" />
      <el-table-column label="邮递编号" align="center" prop="vPostNo" />
      <el-table-column label="邮递方式" align="center" prop="vPostType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.buy_post_type"
            :value="scope.row.vPostType"
          />
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="oprBy" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.inv_arrival_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleExecute(scope.row)"
            v-if="scope.row.status == '01'"
            >执行入库</el-button
          >

          <el-button
            size="mini"
            type="text"
            @click="handlePrint(scope.row)"
            v-if="scope.row.status == '03'"
            >打印入库单</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer title="详情" :visible.sync="arrivalOpen" size="50%" ref="drawer">
      <div class="drawer-container">
        <div class="xin-main">
          <div class="xin-content" style="height: 90%">
            <el-form :model="drawerForm" :disabled="true">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="单据编号" :label-width="formLabelWidth">
                    <el-input
                      v-model="drawerForm.vBillNo"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>
                <el-col :span="12">
                  <el-form-item label="邮递编号" :label-width="formLabelWidth">
                    <el-input
                      v-model="drawerForm.vPostNo"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="邮递方式" :label-width="formLabelWidth">
                    <el-select
                      v-model="drawerForm.vPostType"
                      placeholder="请选择邮递方式"
                      clearable
                    >
                      <el-option
                        v-for="dict in dict.type.buy_post_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      /> </el-select></el-form-item
                ></el-col>
                <el-col :span="12">
                  <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-select
                      v-model="drawerForm.status"
                      placeholder="请选择状态"
                      clearable
                    >
                      <el-option
                        v-for="dict in dict.type.inv_arrival_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      />
                    </el-select> </el-form-item
                ></el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="经办人" :label-width="formLabelWidth">
                    <el-input
                      v-model="drawerForm.oprBy"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>
                <el-col :span="12">
                  <el-form-item label="备注" :label-width="formLabelWidth">
                    <el-input
                      v-model="drawerForm.remark"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>
              </el-row>
            </el-form>
            <el-divider content-position="left"></el-divider>
            <el-table :data="detailList" height="550px" border>
              <el-table-column
                type="index"
                label="序号"
                align="center"
                show-overflow-tooltip
              ></el-table-column>
              <!-- <el-table-column label="物料代码" align="center" prop="vSpa" /> -->
              <el-table-column
                label="物料名称"
                align="center"
                prop="vSpaName"
              />
              <el-table-column
                label="规格型号"
                align="center"
                prop="vSpaModel"
              />
              <el-table-column label="品牌" align="center" prop="vSpaBrand" />
              <el-table-column label="数量" align="center" prop="nQty" />
            </el-table>
            <div
              class="xin-footer"
              style="position: fixed; bottom: 5px; right: 10px"
            >
              <el-button
                type="primary"
                @click="inquiry"
                v-if="this.drawerForm.status == '00'"
                >到货确认</el-button
              >
              <el-button @click="cancelDrawer">取消</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>

    <el-drawer
      title="执行入库"
      :visible.sync="executeOpen"
      size="80%"
      ref="drawer"
    >
      <div class="drawer-container">
        <div class="xin-main">
          <div class="xin-content" style="height: 90%">
            <el-form :model="executeForm" :disabled="true">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="单据编号" :label-width="formLabelWidth">
                    <el-input
                      v-model="executeForm.vBillNo"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>
                <el-col :span="12">
                  <el-form-item label="邮递编号" :label-width="formLabelWidth">
                    <el-input
                      v-model="executeForm.vPostNo"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="邮递方式" :label-width="formLabelWidth">
                    <el-select
                      v-model="executeForm.vPostType"
                      placeholder="请选择邮递方式"
                      clearable
                    >
                      <el-option
                        v-for="dict in dict.type.buy_post_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      /> </el-select></el-form-item
                ></el-col>
                <el-col :span="12">
                  <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-select
                      v-model="executeForm.status"
                      placeholder="请选择状态"
                      clearable
                    >
                      <el-option
                        v-for="dict in dict.type.inv_arrival_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      />
                    </el-select> </el-form-item
                ></el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="经办人" :label-width="formLabelWidth">
                    <el-input
                      v-model="executeForm.oprBy"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>
                <el-col :span="12">
                  <el-form-item label="备注" :label-width="formLabelWidth">
                    <el-input
                      v-model="executeForm.remark"
                      autocomplete="off"
                    ></el-input> </el-form-item
                ></el-col>
              </el-row>
            </el-form>
            <el-divider content-position="left"></el-divider>
            <el-table :data="executeList" height="550px" border>
              <el-table-column
                type="index"
                label="序号"
                align="center"
                show-overflow-tooltip
              ></el-table-column>
              <!-- <el-table-column label="物料代码" align="center" prop="vSpa" /> -->
              <el-table-column
                label="物料名称"
                align="center"
                prop="vSpaName"
              />
              <el-table-column
                label="规格型号"
                align="center"
                prop="vSpaModel"
              />
              <el-table-column label="品牌" align="center" prop="vSpaBrand" />
              <el-table-column label="数量" align="center" prop="nQty" />

              <el-table-column prop="vWHId" label="仓库" align="center">
                <template slot-scope="scope">
                  <el-select
                    size="small"
                    clearable
                    v-model="scope.row.vWHId"
                    @change="handleWHEdit(scope.row)"
                  >
                    <el-option
                      v-for="dict in whList"
                      :key="dict.id"
                      :label="dict.vWHName"
                      :value="dict.id"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="vAreaId" label="库区" align="center">
                <template slot-scope="scope">
                  <el-select
                    size="small"
                    v-model="scope.row.vAreaId"
                    clearable
                    @change="handleAreaEdit(scope.row)"
                  >
                    <el-option
                      v-for="dict in areaList"
                      :key="dict.id"
                      :label="dict.vWHName"
                      :value="dict.id"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="vLocationId" label="库位" align="center">
                <template slot-scope="scope">
                  <el-select
                    size="small"
                    v-model="scope.row.vLocationId"
                    clearable
                    @change="handleLocaltionEdit(scope.row)"
                  >
                    <el-option
                      v-for="dict in localtionList"
                      :key="dict.id"
                      :label="dict.vWHName"
                      :value="dict.id"
                    />
                  </el-select>
                </template>
              </el-table-column>
            </el-table>
            <div
              class="xin-footer"
              style="position: fixed; bottom: 5px; right: 10px"
            >
              <el-button type="primary" @click="executeIn">执行入库</el-button>
              <el-button @click="cancelDrawer">取消</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>
    <el-dialog
      title="打印入库单"
      :visible.sync="printOpen"
      width="50%"
      append-to-body
    >
      <index2 :products="currentPrintInfo" />
    </el-dialog>
  </div>
</template>

<script>
import { listPost } from "@/api/buy/post";
import {
  viewById,
  arrivalComfirm,
  executeIn,
  viewRKBillById,
} from "@/api/inv/inv0004";
import { listInv0001 } from "@/api/inv/inv0001";
import index2 from "./index2";
export default {
  name: "inv0004",
  components: { index2 },
  dicts: ["sys_common_status", "inv_arrival_status", "buy_post_type"],
  data() {
    return {
      flag: false,
      // 遮罩层
      loading: true,
      arrivalOpen: false,
      executeOpen: false,
      printOpen: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购邮寄表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nMainId: null,
        vBillNo: null,
        vPostNo: null,
        vPostType: null,
        oprBy: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      drawerForm: {},
      detailList: [],
      executeForm: {},
      executeList: [],
      formLabelWidth: "120px",
      whList: [],
      localtionList: [],
      areaList: [],
      areaDisabled: false,
      localtionDisabled: false,
      currentPrintInfo: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购邮寄列表 */
    getList() {
      this.loading = true;
      this.queryParams.nMainId = this.nMainid;
      listPost(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleView(row) {
      viewById(row).then((response) => {
        this.drawerForm = response.data.buyPurchasePost;
        this.detailList = response.data.itemPOList;
      });
      this.arrivalOpen = true;
    },

    handleExecute(row) {
      viewById(row).then((response) => {
        this.executeForm = response.data.buyPurchasePost;
        this.executeList = response.data.itemPOList;
      });

      listInv0001({ parentId: "0", whType: "L" }).then((response) => {
        this.whList = response.data;
      });
      this.executeOpen = true;
    },

    cancelDrawer() {
      this.arrivalOpen = false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    inquiry() {
      arrivalComfirm(this.drawerForm).then((response) => {
        if (response.code == "200") {
          this.$modal.msgSuccess("操作成功");
          this.arrivalOpen = false;
          this.queryParams.pageNum = 1;
          this.getList();
        }
      });
    },
    handleWHEdit(row) {
      row.vAreaId = "";
      row.vLocationId = "";
      this.localtionList = [];
      if (row.vWHId != "" && row.vWHId != "undefined") {
        listInv0001({ parentId: row.vWHId, whType: "L" }).then((response) => {
          this.areaList = response.data;
          this.areaDisabled = false;
        });
      } else {
        this.areaList = [];
        this.areaDisabled = true;
        this.localtionDisabled = true;
      }
    },

    handleAreaEdit(row) {
      row.vAreaId = row.vAreaId;
      row.vLocationId = "";
      this.localtionList = [];
      if (row.vAreaId != "" && row.vAreaId != "undefined") {
        listInv0001({ parentId: row.vAreaId, whType: "L" }).then((response) => {
          this.localtionList = response.data;
          this.localtionDisabled = false;
        });
      } else {
        this.localtionDisabled = true;
      }
    },

    handleLocaltionEdit(row) {
      if (row.vAreaId != "" && row.vAreaId != "undefined") {
        listInv0001({ parentId: row.vAreaId, whType: "L" }).then((response) => {
          this.localtionList = response.data;
          row.vLocationId = row.vLocationId;
        });
      }
    },

    executeIn() {
      executeIn({
        postBill: this.executeForm,
        itemPOList: this.executeList,
      }).then((response) => {
        if (response.code == "200") {
          this.$modal.msgSuccess("操作成功");
          this.executeOpen = false;
          this.queryParams.pageNum = 1;
          this.getList();
        }
      });
    },
    handlePrint(row) {
      viewRKBillById(row).then((response) => {
        if (response.code == "200") {
          console.log(response);
          this.currentPrintInfo = response.data;
          this.printOpen = true;
        }
      });
    },
  },
};
</script>
